#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void *func(void *);

int main(int argc, char *argv[]) {
  pthread_t tid;
  int ret;
  ret = pthread_create(&tid, NULL, func, NULL);
  if (ret != 0) {
    perror("pthread_create fail");
    return -1;
  }

  sleep(1);
  pthread_cancel(tid);

  pthread_join(tid,NULL);
  pthread_exit(NULL);
}

void *func(void *argv) {

  int i = 0;
  int ret;
  ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  if (ret != 0) {
    perror("pthread_setcancelstate fail");
    exit(0);
  }
  while (i < 100) {
    if (i == 4) {
      pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    }
    printf("hello world\n");

    sleep(1);
    i++;
  }
  pthread_exit(NULL);
}
